/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.db;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import pl.cezary.butler.walili.server.db.tables.ITableDefinition;
import pl.cezary.butler.walili.server.db.utils.TableDefinitionUtils;

/**
 *
 * @author czarek
 */
public enum Queries {

    SELECT_ZADANIE, SELECT_ZAD_RESZTA, SELECT_ZADANIE_BY_DATE, SELECT_POLOZENIE, SELECT_RESZTA, DELETE_USER_CREDENTIALS, DELETE_USER_ID;
    private static final String propFileName = "queries.properties";
    private static Properties props;

    public static Properties getQueries() {

        //singleton
        if (props == null) {
            InputStream is =
                    Queries.class.getClassLoader().getResourceAsStream(Queries.class.getPackage().getName().replace('.', '/') + "/" + propFileName);
            if (is == null) {
                throw new RuntimeException("Unable to load property file: " + propFileName);
            }
            props = new Properties();
            try {
                props.load(is);
            } catch (IOException e) {
                throw new RuntimeException("Unable to load property file: " + propFileName + "\n" + e.getMessage());
            }
        }
        return props;
    }

    public static String getQuery(String query) {
        return getQueries().getProperty(query);
    }

    public String getQuery() {
        return getQuery(this.name().toLowerCase());
    }

    public static String getInsertQuery(ITableDefinition td) {
        return TableDefinitionUtils.getInsertQuery(td);
    }
}
